package com.thermofisher.service.impl;

import com.thermofisher.beans.Employee;
import com.thermofisher.beans.Home;
import com.thermofisher.beans.HomeData;
import com.thermofisher.beans.SearchProject;
import com.thermofisher.mapper.EmployeeMapper;
import com.thermofisher.mapper.HomeMapper;
import com.thermofisher.service.HomeService;
import com.thermofisher.utils.DataUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService
{

    private static final Logger log = LoggerFactory.getLogger(HomeServiceImpl.class);

    @Autowired
    private HomeMapper HomeMapper;

    @Autowired
    private DataUtil dataUtil;

    @Autowired
    private EmployeeMapper employeeMapper;

    public HomeData getHomeData(SearchProject searchProject)
    {
        HomeData homeData = new HomeData();
        List list = new ArrayList();
        searchProject.setEmail(dataUtil.getEmail(searchProject.getEmail()));
        LocalDate today = LocalDate.now();
        if (!StringUtils.isBlank(searchProject.getMaxEstInitiateDate()))
        {
            today = LocalDate.parse(searchProject.getMaxEstInitiateDate());
            homeData.setEndDate(searchProject.getMaxEstInitiateDate());
        }
        if (!StringUtils.isBlank(searchProject.getMaxEstBidDate()))
        {
            today = LocalDate.parse(searchProject.getMaxEstBidDate());
            homeData.setEndDate(searchProject.getMaxEstBidDate());
        }
        if (!StringUtils.isBlank(searchProject.getMaxEstSignDate()))
        {
            today = LocalDate.parse(searchProject.getMaxEstSignDate());
            homeData.setEndDate(searchProject.getMaxEstSignDate());
        }
        if (!StringUtils.isBlank(searchProject.getMaxEstArrivalDate()))
        {
            today = LocalDate.parse(searchProject.getMaxEstArrivalDate());
            homeData.setEndDate(searchProject.getMaxEstArrivalDate());
        }
        if (!StringUtils.isBlank(searchProject.getMinEstInitiateDate()))
        {
            homeData.setStartDate(searchProject.getMinEstInitiateDate());
        }
        if (!StringUtils.isBlank(searchProject.getMinEstBidDate()))
        {
            homeData.setStartDate(searchProject.getMinEstBidDate());
        }
        if (!StringUtils.isBlank(searchProject.getMinEstSignDate()))
        {
            homeData.setStartDate(searchProject.getMinEstSignDate());
        }
        if (!StringUtils.isBlank(searchProject.getMinEstArrivalDate()))
        {
            homeData.setStartDate(searchProject.getMinEstArrivalDate());
        }
        LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
        for (int i = 1; i < 13; i++)
        {
            LocalDate next = previousYear.plus(i, ChronoUnit.MONTHS);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            list.add(formatter.format(next));
        }
        homeData.setMonthList(list);
        log.info("=============" + searchProject.getEmail());
        String allIds = getAllSubIds(searchProject.getEmail());
        searchProject.setEmail(allIds);
        searchProject = dataUtil.getSearchProject(searchProject);
        List<Home> statusList = HomeMapper.getStatus(searchProject);
        List<Home> typeList = HomeMapper.getTypes(searchProject);
        List<Home> departMentList = HomeMapper.getDepartMent(searchProject);
        List<Home> businessList = HomeMapper.getBusiness(searchProject);
        List<Home> phaseList = HomeMapper.getPhase(searchProject);
        List<Home> companyScala = HomeMapper.getCompanyScala(searchProject);
        List<Home> departJoin = HomeMapper.getDepartJoin(searchProject);
        List<Home> dataByMonthList = HomeMapper.getDataByMonth(searchProject);
        Home partProject = HomeMapper.getPartProject(searchProject);
        Home allProject = HomeMapper.getAllProject(searchProject);
        Home partRate = getPartRate(partProject, allProject);
        Home allDataPercent = getAllDataPercent(searchProject);
        Home partDataPercent = getPartDataPercent(searchProject);
        //homeData.setEmail(allIds);
        homeData.setStatusList(statusList);
        homeData.setTypeList(typeList);
        homeData.setDepartMentList(departMentList);
        homeData.setBusinessList(businessList);
        homeData.setPhaseList(phaseList);
        homeData.setCompanyScala(companyScala);
        homeData.setDepartJoin(departJoin);
        homeData.setDataByMonthList(dataByMonthList);
        homeData.setPartProject(partProject);
        homeData.setAllProject(allProject);
        homeData.setPartRate(partRate);
        homeData.setAllDataPercent(allDataPercent);
        homeData.setPartDataPercent(partDataPercent);
        homeData.setClientBusinessList(HomeMapper.getClientBusiness(searchProject));
        return homeData;
    }

    public String getAllSubIds(String email)
    {
        if (StringUtils.isBlank(email))
        {
            return "";
        }
        StringBuilder strIds = new StringBuilder();
        List<Employee> employeeList = employeeMapper.getEmployeeByEmail(email);
        if (employeeList.size() > 0)
        {
            strIds.append(employeeList.get(0).getEmployeeNumber());
            String subIds = employeeMapper.getAllSubIds(employeeList.get(0).getEmployeeNumber());
            if (StringUtils.isNotBlank(subIds))
            {
                strIds.append(",").append(subIds);
            }
        }
        return strIds.toString();
    }

    private Home getPartRate(Home partProject, Home allProject)
    {
        Home home = new Home();
        if (partProject.getTotal().equals("0"))
        {
            home.setTotal("0%");
        }
        else if (allProject.getTotal().equals("0"))
        {
            home.setTotal("100%");
        }
        else
        {
            double part = Double.valueOf(partProject.getTotal());
            double all = Double.valueOf(allProject.getTotal());
            double total = part / all * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setTotal(totalStr + "%");
        }

        if (null == partProject.getEstAmount() || partProject.getEstAmount().equals("0"))
        {
            home.setEstAmount("0%");
        }
        else if (null == allProject.getEstAmount() || allProject.getEstAmount().equals("0"))
        {
            home.setEstAmount("100%");
        }
        else
        {
            String partStr = getDate(partProject.getEstAmount());
            String allStr = getDate(allProject.getEstAmount());
            double total = Double.valueOf(partStr) / Double.valueOf(allStr) * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setEstAmount(totalStr + "%");
        }

        if (null == partProject.getBooking() || partProject.getBooking().equals("0"))
        {
            home.setBooking("0%");
        }
        else if (null == allProject.getBooking() || allProject.getBooking().equals("0"))
        {
            home.setBooking("100%");
        }
        else
        {
            String partStr = getDate(partProject.getBooking());
            String allStr = getDate(allProject.getBooking());
            double total = Double.valueOf(partStr) / Double.valueOf(allStr) * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setBooking(totalStr + "%");
        }

        if (null == partProject.getRevenue() || partProject.getRevenue().equals("0"))
        {
            home.setRevenue("0%");
        }
        else if (null == allProject.getRevenue() || allProject.getRevenue().equals("0"))
        {
            home.setRevenue("100%");
        }
        else
        {
            String partStr = getDate(partProject.getRevenue());
            String allStr = getDate(allProject.getRevenue());
            double total = Double.valueOf(partStr) / Double.valueOf(allStr) * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setRevenue(totalStr + "%");
        }
        return home;
    }

    public String getDate(String part)
    {
        long data = Long.parseLong(part.substring(0, part.length() - 1));
        if (StringUtils.contains(part, "M"))
        {
            data = data * 1000 * 1000;
        }
        else if (StringUtils.contains(part, "K"))
        {
            data = data * 1000;
        }
        else if (StringUtils.contains(part, "B"))
        {
            data = data * 1000 * 1000 * 1000;
        }
        else
        {

        }
        return String.valueOf(data);
    }

    public Home getAllDataPercent(SearchProject searchProject)
    {
        Home nowData = HomeMapper.getNowAllProject(searchProject);
        Home lastData = HomeMapper.getLastAllProject(searchProject);
        Home Home = getDataPercent(nowData, lastData);
        return Home;
    }

    public Home getPartDataPercent(SearchProject searchProject)
    {
        Home nowData = HomeMapper.getNowPartProject(searchProject);
        Home lastData = HomeMapper.getLastPartProject(searchProject);
        Home Home = getDataPercent(nowData, lastData);
        return Home;
    }

    public Home getDataPercent(Home nowData, Home lastData)
    {
        Home home = new Home();
        log.info("===nowData====" + nowData.toString());
        log.info("===lastData====" + lastData.toString());
        if (nowData.getTotal().equals("0"))
        {
            home.setTotal("0%");
        }
        else if (lastData.getTotal().equals("0"))
        {
            home.setTotal("100%");
        }
        else
        {
            double nowTotal = Double.valueOf(nowData.getTotal());
            double lastTotal = Double.valueOf(lastData.getTotal());
            double total = (nowTotal - lastTotal) / lastTotal * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setTotal(totalStr + "%");
        }

        if (null == nowData.getEstAmount() || nowData.getEstAmount().equals("0"))
        {
            home.setEstAmount("0%");
        }
        else if (null == lastData.getEstAmount() || lastData.getEstAmount().equals("0"))
        {
            home.setEstAmount("100%");
        }
        else
        {
            double nowTotal = Double.valueOf(nowData.getEstAmount());
            double lastTotal = Double.valueOf(lastData.getEstAmount());
            double total = (nowTotal - lastTotal) / lastTotal * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setEstAmount(totalStr + "%");
        }

        if (null == nowData.getBooking() || nowData.getBooking().equals("0"))
        {
            home.setBooking("0%");
        }
        else if (null == lastData.getBooking() || lastData.getBooking().equals("0"))
        {
            home.setBooking("100%");
        }
        else
        {
            double nowTotal = Double.valueOf(nowData.getBooking());
            double lastTotal = Double.valueOf(lastData.getBooking());
            double total = (nowTotal - lastTotal) / lastTotal * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setBooking(totalStr + "%");
        }

        if (null == nowData.getRevenue() || nowData.getRevenue().equals("0"))
        {
            home.setRevenue("0%");
        }
        else if (null == lastData.getRevenue() || lastData.getRevenue().equals("0"))
        {
            home.setRevenue("100%");
        }
        else
        {
            double nowTotal = Double.valueOf(nowData.getRevenue());
            double lastTotal = Double.valueOf(lastData.getRevenue());
            double total = (nowTotal - lastTotal) / lastTotal * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setRevenue(totalStr + "%");
        }

        if (null == nowData.getPercent() || nowData.getPercent().equals("0.00"))
        {
            home.setPercent("0%");
        }
        else if (null == lastData.getPercent() || lastData.getPercent().equals("0.00"))
        {
            home.setPercent("100%");
        }
        else
        {
            double nowTotal = Double.valueOf(nowData.getPercent());
            double lastTotal = Double.valueOf(lastData.getPercent());
            double total = (nowTotal - lastTotal) / lastTotal * 100;
            String totalStr = String.valueOf(Math.round(total));
            home.setPercent(totalStr + "%");
        }
        return home;
    }

}
